﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _classajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            id = Str2Str(q("id"));
            Admin_Load("", "json", true);
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "move":
                    ajaxMove();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                case "createnav":
                    ajaxCreateNav();
                    break;
                case "createouturl":
                    ajaxCreateOuturl();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            this._response = JsonResult(1, "可以重复");
        }
        private void ajaxGetList()
        {
            Admin_Load(ChannelId + "-08", "json");
            doh.Reset();
            doh.SqlCmd = "SELECT [ID],[Title],[Number],[Code],[IsOut],len(Code) as codelength,[ChannelId],[IsTop] FROM [jcms_normal_class] WHERE [ChannelId]=" + ChannelId + " ORDER BY code";
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"操作成功\"," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
        private void ajaxDel()
        {
            Admin_Load(ChannelId + "-08", "json");
            string cId = f("id");
            doh.Reset();
            doh.ConditionExpress = "id=" + cId;
            string cCode = doh.GetValue("jcms_normal_class", "code").ToString();
            doh.Reset();
            doh.ConditionExpress = " [ChannelId]= " + ChannelId + "AND [code] LIKE '" + cCode + "%' and len(code)>" + cCode.Length;
            bool isDel = doh.Exist("jcms_normal_class");
            if (isDel)
            {
                this._response = JsonResult(0, "含有子栏,不可删");
                return;
            }
            if (ChannelType.ToLower() != "system")
            {
                doh.Reset();
                doh.ConditionExpress = "[ClassId]=" + cId;
                isDel = doh.Exist("jcms_module_" + ChannelType);
                if (isDel)
                {
                    this._response = JsonResult(0, "含有内容,不可删");
                    return;
                }
            }
            DeleteClassFile(cId, true);//删除静态文件
            doh.Reset();
            doh.ConditionExpress = "id=" + cId;
            doh.Delete("jcms_normal_class");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxMove()
        {
            string id = f("id");
            string isUp = f("up");
            if (id == "0")
            {
                this._response = JsonResult(0, "ID错误");
                return;
            }
            doh.Reset();
            doh.ConditionExpress = "id=" + id;
            string oldCode = doh.GetValue("jcms_normal_class", "code").ToString();
            int codeLen = oldCode.Length;
            string subStr = DBType == "0" ? "mid" : "substring";
            if (codeLen > 1)
            {
                string temp = string.Empty;
                string wStr = "";
                string newStr = "";
                for (int i = 0; i < codeLen; i++)
                    newStr += "-";
                if (codeLen > 4)
                    wStr = " and left(code," + Convert.ToString(codeLen - 4) + ")='" + oldCode.Substring(0, codeLen - 4) + "'";

                if (isUp == "1")
                    wStr = "SELECT TOP 1 code FROM [jcms_normal_class] WHERE len(code)=" + codeLen.ToString() + " and code<'" + oldCode + "'" + wStr + " ORDER BY code desc";
                else
                    wStr = "SELECT TOP 1 code FROM [jcms_normal_class] WHERE len(code)=" + codeLen.ToString() + " and code>'" + oldCode + "'" + wStr + " ORDER BY code";
                doh.Reset();
                doh.SqlCmd = wStr;
                DataTable dtClass = doh.GetDataTable();
                if (dtClass.Rows.Count > 0)
                    temp = dtClass.Rows[0]["code"].ToString();

                if (temp.Length > 1)
                {
                    //Move Under Class
                    wStr = "UPDATE [jcms_normal_class] SET [code]='" + newStr + "'+" + subStr + "(code," + Convert.ToString(codeLen + 1) + ",len(code)) where left(code," + codeLen.ToString() + ")='" + temp + "'";
                    doh.Reset();
                    doh.SqlCmd = wStr;
                    doh.ExecuteSqlNonQuery();

                    //Update Target Class
                    wStr = "UPDATE [jcms_normal_class] SET [code]='" + temp + "'+" + subStr + "(code," + Convert.ToString(codeLen + 1) + ",len(code)) where left(code," + codeLen.ToString() + ")='" + oldCode + "'";
                    doh.Reset();
                    doh.SqlCmd = wStr;
                    doh.ExecuteSqlNonQuery();

                    //Update Under Class
                    wStr = "UPDATE [jcms_normal_class] SET [code]='" + oldCode + "'+" + subStr + "(code," + Convert.ToString(newStr.Length + 1) + ",len(code)) where left(code," + newStr.Length.ToString() + ")='" + newStr + "'";
                    doh.Reset();
                    doh.SqlCmd = wStr;
                    doh.ExecuteSqlNonQuery();

                }
                dtClass.Clear();
                dtClass.Dispose();
            }
            this._response = JsonResult(1, "成功移动");
        }
        private void ajaxCreateNav()
        {
            CreateMenu(ChannelId);
            CreateMenu("0");
            CreateNavigate(ChannelId);
            this._response = JsonResult(1, "栏目导航更新完成");
        }
        private void ajaxCreateOuturl()
        {
            string ids = f("ids");
            doh.Reset();
            doh.SqlCmd = "SELECT id,OutUrl FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + ChannelId + " AND [Id] in (" + ids + ") AND len(OutUrl)>0";
            DataTable dt = doh.GetDataTable();
            if (dt.Rows.Count > 0)
            {
                string TempStr = string.Empty;
                string OutStr = string.Empty;
                if (System.IO.File.Exists(Server.MapPath(site.Dir) + "controls\\class.aspx"))
                {
                    TempStr = System.IO.File.ReadAllText(Server.MapPath(site.Dir) + "controls\\class.aspx");
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    OutStr = TempStr.Replace("{$ChannelId}", ChannelId).Replace("{$ClassId}", dt.Rows[i]["Id"].ToString());
                    Jumbot.Common.DirFile.SaveFile(OutStr, dt.Rows[i]["OutUrl"].ToString());
                }
            }
            this._response = JsonResult(1, "生成成功");
        }
    }
}